#define FLASH_FID                           (8UL << 6 | 11UL)       /* Definition for mtl_Log() */
/***********************************************************************************************
* DISCLAIMER
************************************************************************************************/
/*""FILE COMMENT""******************************************************************************
 * System Name  : FLASH driver software
 * File Name    : w25q_usr.c
 * Version      : 
 * Contents     : FLASH driver user I/F module
 * Customer     : Standard
 * Model        : None
 * CPU          : 
 *              : 
 * Compiler     : 
 *              : 
 * OS           : ---
 * Programmer   : 
 * Note         : 
 ***********************************************************************************************
 * History      : Date          Author          Comment
 *""FILE COMMENT END""**************************************************************************/

#include <stddef.h>
#include "w25q.h"                   /* FLASH driver common definitions              */
#include "w25q_io.h"                /* FLASH driver I/O module definitions          */

/************************************************************************************************/
/*  Definitions                                                                                 */
/************************************************************************************************/


/************************************************************************************************/
/*  Unions                                                                                      */
/************************************************************************************************/


/************************************************************************************************/
/*  Structs                                                                                     */
/************************************************************************************************/


/************************************************************************************************/
/*  Globals                                                                                     */
/************************************************************************************************/


/************************************************************************************************/
/*  Macros                                                                                      */
/************************************************************************************************/


/************************************************************************************************/
/*  Prototypes                                                                                  */
/************************************************************************************************/

/*""FUNC COMMENT""*******************************************************************************
 * Outline      : Flash driver initialization processing
 *-----------------------------------------------------------------------------------------------
 * Declaration  : error_t SPI_FLASH_Init_Driver(void)
 *-----------------------------------------------------------------------------------------------
 * Functions    : Initializes the Flash driver.
 *              : Initializes the SFR for Flash control.
 *              : Performs the following processing for each device.
 *              :       Setting of Flash control ports.
 *              : Call this function once at system activation
 *-----------------------------------------------------------------------------------------------
 * Return Value : SIO_OK                                ;   Successful operation
 *              : FLASH_ERR_OTHER                       ;   Hardware error
 *-----------------------------------------------------------------------------------------------
 * Return values: None
 *-----------------------------------------------------------------------------------------------
 * Notes        : None
 *""FUNC COMMENT END""***************************************************************************/
error_t SPI_FLASH_Init_Driver(void)
{
    uint8_t             DevNo;                          /* Device number                        */

    for (DevNo = FLASH_DEV0; DevNo < FLASH_DEV_NUM; DevNo++) {
        SPI_FLASH_Init_Port(DevNo);                   /* CS initialization                    */
    }

    return SIO_Init_Driver();                         /* Driver initialization                */
}


/*""FUNC COMMENT""*******************************************************************************
 * Outline      : Status read processing
 *-----------------------------------------------------------------------------------------------
 * Declaration  : error_t SPI_FLASH_Read_Status(uint8_t DevNo, uint8_t FAR* pStatus)
 *-----------------------------------------------------------------------------------------------
 * Functions    : Reads the status.
 *              : Reads from the status register.
 *              : The following information is stored in the read status storage buffer 
 *              : (pStatus).
 *              :
 *              :       Bit 7:SRWD
 *              :                 1: BP2, BP1, BP0 are read-only bits
 *              :                 0: BP2, BP1, BP0 are read/writable
 *              :       Bits 6 to 5:Reserved (All "0")
 *              :       Bits 4 to 2:BP2, BP1, BP0
 *              :       Bit 1:WEL
 *              :                 1: Internal Write Enable Latch is set
 *              :                 0: Internal Write Enable Latch is reset
 *              :       Bit 0:WIP
 *              :                 1: Program or Erase cycle is in progress
 *              :                 0: No Program or Erase cycle is in progress
 *-----------------------------------------------------------------------------------------------
 * Arguments    : uint8_t           DevNo               ;   Device number
 *              : uint8_t FAR*      pStatus             ;   Read status storage buffer
 *-----------------------------------------------------------------------------------------------
 * Return values: Returns the status register acquisition result.
 *              : FLASH_OK                              ;   Successful operation
 *              : FLASH_ERR_PARAM                       ;   Parameter error
 *              : FLASH_ERR_HARD                        ;   Hardware error
 *              : FLASH_ERR_OTHER                       ;   Other error
 *-----------------------------------------------------------------------------------------------
 * Notes        : Refer the specification of use Flash about relation between block
 *              : protection bits(BP0,BP1 and BP2) and protection area.
 *              : The information of BP2 bit isn't output from the Flash which status
 *              : register doesn't include of BP2 bit.
 *""FUNC COMMENT END""***************************************************************************/
error_t SPI_FLASH_Read_Status(uint8_t DevNo, uint8_t FAR* pStatus)
{
    error_t             Ret;

    /* Parameter check */
    if ((DevNo   >= FLASH_DEV_NUM)
     || (pStatus == NULL       )) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return FLASH_ERR_PARAM;
    }

    SPI_FLASH_Init_Port(DevNo);                       /* Port initialization                  */
    Ret = SIO_Enable(FLASH_BR);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }

    /* Status register reading */
    Ret = SPI_FLASH_Read_StsReg(DevNo, pStatus);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }
    Ret = SIO_Disable();                              /* SFR initialization                   */
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return Ret;
    }
    
    return Ret;
}


/*""FUNC COMMENT""*******************************************************************************
 * Outline      : Write-protection setting processing
 *-----------------------------------------------------------------------------------------------
 * Declaration  : error_t SPI_FLASH_Write_Protect(uint8_t DevNo, uint8_t WpSts)
 *-----------------------------------------------------------------------------------------------
 * Functions    : Makes the write-protection setting.
 *              : The BP0,BP1 and BP2 bit of status register is set as follows by 
 *              : write-protection setting data (WpSts).
 *              :   WpSts=0 :   BP0=0   BP1=0,  BP2=0
 *              :   WpSts=1 :   BP0=1   BP1=0,  BP2=0
 *              :   WpSts=2 :   BP0=0   BP1=1,  BP2=0
 *              :   WpSts=3 :   BP0=1   BP1=1,  BP2=0
 *              :   WpSts=4 :   BP0=0   BP1=0,  BP2=1
 *              :   WpSts=5 :   BP0=1   BP1=0,  BP2=1
 *              :   WpSts=6 :   BP0=0   BP1=1,  BP2=1
 *              :   WpSts=7 :   BP0=1   BP1=1,  BP2=1
 *-----------------------------------------------------------------------------------------------
 * Arguments    : uint8_t           DevNo               ;   Device number
 *              : uint8_t           WpSts               ;   Write-protection setting data
 *-----------------------------------------------------------------------------------------------
 * Return values: Returns the write-protection setting result.
 *              : FLASH_OK                              ;   Successful operation
 *              : FLASH_ERR_PARAM                       ;   Parameter error
 *              : FLASh_ERR_OTHER                       ;   Other error
 *-----------------------------------------------------------------------------------------------
 * Notes        : SRWD is fixed 0.
 *              : The Flash not assigned BP2 bit to status register should be set the
 *              : WpSts among 0 to 3. 
 *""FUNC COMMENT END""***************************************************************************/
error_t SPI_FLASH_Write_Protect(uint8_t DevNo, uint8_t WpSts)
{
    uint8_t             StsReg;                         /* Write status buffer                  */
    error_t             Ret;

    /* Parameter check */
    if ((DevNo >= FLASH_DEV_NUM     )
     || (WpSts >  FLASH_WP_WHOLE_MEM)) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return FLASH_ERR_PARAM;
    }

    SPI_FLASH_Init_Port(DevNo);                       /* Port initialization                  */
    Ret = SIO_Enable(FLASH_BR);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }

    /* Write-protection setting value storage */
    StsReg = (WpSts << 2) & (~FLASH_REG_SRWD);          /* SRWD is fixed "0"                    */

    /* Status register writing */
    Ret = SPI_FLASH_Write_StsReg(DevNo, (uint8_t FAR*)&StsReg);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }
    Ret = SIO_Disable();                              /* SFR initialization                   */
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return Ret;
    }
    
    return Ret;
}


/*""FUNC COMMENT""*******************************************************************************
 * Outline      : Data read processing
 *-----------------------------------------------------------------------------------------------
 * Declaration  : error_t SPI_FLASH_Read_Data(uint8_t DevNo, uint32_t RAddr, uint32_t RCnt, uint8_t FAR* pData)
 *-----------------------------------------------------------------------------------------------
 * Functions    : Reads data from Flash in bytes.
 *              : Reads data from the specified address for the specified number of bytes.
 *-----------------------------------------------------------------------------------------------
 * Arguments    : uint8_t           DevNo               ;   Device number
 *              : uint32_t          RAddr               ;   Read start address
 *              : uint32_t          RCnt                ;   Number of bytes to be read
 *              : uint8_t FAR*      pData               ;   Read data storage buffer pointer
 *-----------------------------------------------------------------------------------------------
 * Return values: Returns the read result.
 *              : FLASH_OK                              ;   Successful operation
 *              : FLASH_ERR_PARAM                       ;   Parameter error
 *              : FLASH_ERR_HARD                        ;   Hardware error
 *              : FLASH_ERR_OTHER                       ;   Other error
 *-----------------------------------------------------------------------------------------------
 * Notes        : The maximum read address is Flash size - 1.
 *""FUNC COMMENT END""***************************************************************************/
error_t SPI_FLASH_Read_Data(uint8_t DevNo, uint32_t RAddr, uint32_t RCnt, uint8_t FAR* pData)
{
    error_t             Ret;

    /* Parameter check */
    if ((DevNo          >= FLASH_DEV_NUM )
     || (pData          == NULL        )) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return FLASH_ERR_PARAM;
    }
    if ((RCnt           == 0           )
     || ((RAddr + RCnt) >  FLASH_MEM_SIZE)) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return FLASH_ERR_PARAM;
    }

    SPI_FLASH_Init_Port(DevNo);                       /* Port initialization                  */
    Ret = SIO_Enable(FLASH_BR);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }

    /* Read command transmission */
    Ret = SPI_FLASH_Read_Memory(DevNo, RAddr, RCnt, pData);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }
    Ret = SIO_Disable();                              /* SFR initialization                   */
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return Ret;
    }
    
    return Ret;
}


/*""FUNC COMMENT""*******************************************************************************
 * Outline      : Data write processing
 *-----------------------------------------------------------------------------------------------
 * Declaration  : error_t SPI_FLASH_Write_Data(uint8_t DevNo, uint32_t WAddr, uint32_t WCnt,
 *              :                        uint8_t FAR* pData)
 *-----------------------------------------------------------------------------------------------
 * Functions    : Writes data to Flash in bytes.
 *              : Writes data from the specified address for the specified number of bytes.
 *-----------------------------------------------------------------------------------------------
 * Arguments    : uint8_t           DevNo               ;   Device number
 *              : uint32_t          WAddr               ;   Write start address
 *              : uint32_t          WCnt                ;   Number of bytes to be written
 *              : uint8_t FAR*      pData               ;   Write data storage buffer pointer
 *-----------------------------------------------------------------------------------------------
 * Return values: Returns the write result.
 *              : FLASH_OK                              ;   Successful operation
 *              : FLASH_ERR_PARAM                       ;   Parameter error
 *              : FLASH_ERR_HARD                        ;   Hardware error
 *              : FLASH_ERR_OTHER                       ;   Other error
 *-----------------------------------------------------------------------------------------------
 * Notes        : Flash can be written to only when write-protection has been canceled.
 *              : Data can't be written to protected pages and error result isn't returned.
 *              : The maximum write address is Flash size - 1.
 *""FUNC COMMENT END""***************************************************************************/
error_t SPI_FLASH_Write_Data(uint8_t DevNo, uint32_t WAddr, uint32_t WCnt, uint8_t FAR* pData)
{
    uint32_t            WByteCnt;                       /* 1page writing byte count             */
    uint32_t            TmpCnt;
    error_t             Ret;

    /* Parameter check */
    if ((DevNo    >= FLASH_DEV_NUM )
     || (pData    == NULL        )) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return FLASH_ERR_PARAM;
    }
    if ((WCnt     == 0           )
     || ((WAddr + WCnt) >  FLASH_MEM_SIZE)) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return FLASH_ERR_PARAM;
    }

    SPI_FLASH_Init_Port(DevNo);                       /* Port initialization                  */
    Ret = SIO_Enable(FLASH_BR);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }

    /* Writing page calculation */
    TmpCnt = ((WAddr + FLASH_WPAG_SIZE) / FLASH_WPAG_SIZE) * FLASH_WPAG_SIZE - WAddr;

    do{
        if (WCnt < TmpCnt) {
            WByteCnt = WCnt;
        } else {
            WByteCnt = TmpCnt;
        }
        
        Ret = SPI_FLASH_Write_Page(DevNo, WAddr, WByteCnt, pData);
        if (Ret < FLASH_OK) {
            SIO_Disable();                            /* SFR initialization                   */
            mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
            return Ret;
        }

        /* Next writing address calculation */
        WCnt  -= WByteCnt;
        pData += WByteCnt;
        WAddr += WByteCnt;
        TmpCnt = FLASH_WPAG_SIZE;
        
    }while(WCnt);

    Ret = SIO_Disable();                              /* SFR initialization                   */
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return Ret;
    }

    return FLASH_OK;
}


/*""FUNC COMMENT""*******************************************************************************
 * Outline      : Bulk erase processing
 *-----------------------------------------------------------------------------------------------
 * Declaration  : error_t SPI_FLASH_BulkErase(uint8_t DevNo)
 *-----------------------------------------------------------------------------------------------
 * Functions    : Erase the all data of Flash.
 *-----------------------------------------------------------------------------------------------
 * Arguments    : uint8_t           DevNo               ;   Device number
 *-----------------------------------------------------------------------------------------------
 * Return values: Returns the bulk erase result.
 *              : FLASH_OK                              ;   Successful operation
 *              : FLASH_ERR_PARAM                       ;   Parameter error
 *              : FLASH_ERR_HARD                        ;   Hardware error
 *              : FLASH_ERR_OTHER                       ;   Other error
 *-----------------------------------------------------------------------------------------------
 * Notes        : Flash can be erased to only when write-protection has been canceled.
 *              : When the Flash is Write- protected, it can't be erased and error 
 *              : result isn't returned.
 *""FUNC COMMENT END""***************************************************************************/
error_t SPI_FLASH_BulkErase(uint8_t DevNo)
{
    error_t             Ret;

    /* Parameter check */
    if (DevNo >= FLASH_DEV_NUM) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return FLASH_ERR_PARAM;
    }

    SPI_FLASH_Init_Port(DevNo);                       /* Port initialization                  */
    Ret = SIO_Enable(FLASH_BR);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }

    /* Erasing */
    Ret = SPI_FLASH_Erase(DevNo, (uint32_t)0, FLASH_B_ERASE);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }
    Ret = SIO_Disable();                              /* SFR initialization                   */
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return Ret;
    }
    
    return Ret;
}


/*""FUNC COMMENT""*******************************************************************************
 * Outline      : Sector erase processing
 *-----------------------------------------------------------------------------------------------
 * Declaration  : error_t SPI_FLASH_SectorErase(uint8_t DevNo, uint32_t EAddr)
 *-----------------------------------------------------------------------------------------------
 * Functions    : Erase the sector data of specified Flash.
 *-----------------------------------------------------------------------------------------------
 * Arguments    : uint8_t           DevNo               ;   Device number
 *              : uint32_t          EAddr               ;   Erase address
 *-----------------------------------------------------------------------------------------------
 * Return values: Returns the sector erase result.
 *              : FLASH_OK                              ;   Successful operation
 *              : FLASH_ERR_PARAM                       ;   Parameter error
 *              : FLASH_ERR_HARD                        ;   Hardware error
 *              : FLASH_ERR_OTHER                       ;   Other error
 *-----------------------------------------------------------------------------------------------
 * Notes        : Flash can be erased to only when write-protection has been canceled.
 *              : Data of protected sector can't be erased and error result isn't returned.
 *""FUNC COMMENT END""***************************************************************************/
error_t SPI_FLASH_SectorErase(uint8_t DevNo, uint32_t EAddr)
{
    error_t             Ret;

    /* Parameter check */
    if (DevNo >= FLASH_DEV_NUM) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return FLASH_ERR_PARAM;
    }
    
    if (EAddr >=  FLASH_MEM_SIZE) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return FLASH_ERR_PARAM;
    }

    SPI_FLASH_Init_Port(DevNo);                       /* Port initialization                  */
    Ret = SIO_Enable(FLASH_BR);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }

    EAddr &= FLASH_SECT_ADDR;                           /*Sector address setting                */
    /* Erasing */
    Ret = SPI_FLASH_Erase(DevNo, EAddr, FLASH_S_ERASE);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }
    Ret = SIO_Disable();                              /* SFR initialization                   */
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return Ret;
    }
    
    return Ret;
}


/*""FUNC COMMENT""*******************************************************************************
 * Outline      : Deep power down processing
 *-----------------------------------------------------------------------------------------------
 * Declaration  : error_t SPI_FLASH_DeepPDown(uint8_t DevNo)
 *-----------------------------------------------------------------------------------------------
 * Functions    : Set the deep power down mode.
 *-----------------------------------------------------------------------------------------------
 * Arguments    : uint8_t           DevNo               ;   Device number
 *-----------------------------------------------------------------------------------------------
 * Return values: Returns the deep power down result.
 *              : FLASH_OK                              ;   Successful operation
 *              : FLASH_ERR_PARAM                       ;   Parameter error
 *              : FLASH_ERR_HARD                        ;   Hardware error
 *              : FLASH_ERR_OTHER                       ;   Other error
 *-----------------------------------------------------------------------------------------------
 * Notes        : Flash may not support the this instruction.
 *""FUNC COMMENT END""***************************************************************************/
error_t SPI_FLASH_DeepPDown(uint8_t DevNo)
{
    error_t             Ret;

    /* Parameter check */
    if (DevNo >= FLASH_DEV_NUM ) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return FLASH_ERR_PARAM;
    }

    SPI_FLASH_Init_Port(DevNo);                       /* Port initialization                  */
    Ret = SIO_Enable(FLASH_BR);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }

    /* Read command transmission */
    Ret = SPI_FLASH_DPD(DevNo);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }
    Ret = SIO_Disable();                              /* SFR initialization                   */
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return Ret;
    }
    
    mtl_wait_lp(FLASH_T_DP_WAIT);

    return Ret;
}


/*""FUNC COMMENT""*******************************************************************************
 * Outline      : Release deep power down processing
 *-----------------------------------------------------------------------------------------------
 * Declaration  : error_t SPI_FLASH_ReleaseDeepPDown(uint8_t DevNo, uint8_t FAR* pData)
 *-----------------------------------------------------------------------------------------------
 * Functions    : Change mode from deep power down to standby.
 *              : Read electronic signature.
 *-----------------------------------------------------------------------------------------------
 * Arguments    : uint8_t           DevNo               ;   Device number
 *              : uint8_t FAR*      pData               ;   Electronic signature storage buffer pointer
 *-----------------------------------------------------------------------------------------------
 * Return values: Returns the release deep power down result.
 *              : FLASH_OK                              ;   Successful operation
 *              : FLASH_ERR_PARAM                       ;   Parameter error
 *              : FLASH_ERR_HARD                        ;   Hardware error
 *              : FLASH_ERR_OTHER                       ;   Other error
 *-----------------------------------------------------------------------------------------------
 * Notes        : Flash may not support the this instruction.
 *""FUNC COMMENT END""***************************************************************************/
error_t SPI_FLASH_ReleaseDeepPDown(uint8_t DevNo, uint8_t FAR* pData)
{
    error_t             Ret;

    /* Parameter check */
    if ((DevNo          >= FLASH_DEV_NUM )
     || (pData          == NULL        )) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return FLASH_ERR_PARAM;
    }

    SPI_FLASH_Init_Port(DevNo);                       /* Port initialization                  */
    Ret = SIO_Enable(FLASH_BR);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }

    /* Read command transmission */
    Ret = SPI_FLASH_ReleaseDPD(DevNo, pData);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }
    Ret = SIO_Disable();                              /* SFR initialization                   */
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return Ret;
    }
    
    mtl_wait_lp(FLASH_T_RES_WAIT);
    
    return Ret;
}


/*""FUNC COMMENT""*******************************************************************************
 * Outline      : ID read processing
 *-----------------------------------------------------------------------------------------------
 * Declaration  : error_t SPI_FLASH_ReadID(uint8_t DevNo, uint8_t FAR* pData)
 *-----------------------------------------------------------------------------------------------
 * Functions    : Read Manufacture ID and Device ID.
 *-----------------------------------------------------------------------------------------------
 * Arguments    : uint8_t           DevNo               ;   Device number
 *              : uint8_t FAR*      pData               ;   ID data storage buffer pointer
 *              : ID data of 3 bytes are stored in the following order.
 *              :                               (1) Manufacture ID
 *              :                               (2) Memory type
 *              :                               (3) Memory capacity
 *-----------------------------------------------------------------------------------------------
 * Return values: Returns the ID read result.
 *              : FLASH_OK                              ;   Successful operation
 *              : FLASH_ERR_PARAM                       ;   Parameter error
 *              : FLASH_ERR_HARD                        ;   Hardware error
 *              : FLASH_ERR_OTHER                       ;   Other error
 *-----------------------------------------------------------------------------------------------
 * Notes        : None
 *""FUNC COMMENT END""***************************************************************************/
error_t SPI_FLASH_ReadID(uint8_t DevNo, uint8_t FAR* pData )
{
    error_t             Ret;
    
    /* Parameter check */
    if ((DevNo          >= FLASH_DEV_NUM )
     || (pData          == NULL        )) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return FLASH_ERR_PARAM;
    }

    SPI_FLASH_Init_Port(DevNo);                       /* Port initialization                  */
    Ret = SIO_Enable(FLASH_BR);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }

    /* Read command transmission */
    Ret = SPI_FLASH_ID(DevNo, pData);
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        SIO_Disable();
        return Ret;
    }
    Ret = SIO_Disable();                              /* SFR initialization                   */
    if (Ret < FLASH_OK) {
        mtl_Log(FLASH_LOG_ERR, 0, FLASH_FID, __LINE__);
        return Ret;
    }
    
    return Ret;

}

/* End of File */
